<template>
  <el-card class="account-container">
    <h1 :style="`font-size: var(--el-font-size-extra-large)`" style="font-size: 26px; color: #615f5f">样式管理</h1>
    <h2 :style="`font-size: var(--el-font-size-extra-large)`" style="font-size: 26px; color: #615f5f">首页轮播图设置</h2>
    <el-form-item label="请选择商家">
      <el-select v-model="value" class="m-2" placeholder="Select" size="large" @change="check">
        <el-option
            v-for="item in merchants"
            :key="item.id"
            :label="item.shopName"
            :value="item.id"
        />
      </el-select>
    </el-form-item>
    <el-divider />
    <el-upload
        v-model:file-list="fileList"
        action="/api/upload/image"
        name="imageFile"
        list-type="picture-card"
        :on-preview="handlePictureCardPreview"
    >
      <el-icon><Plus /></el-icon>
    </el-upload>
    <el-divider />
    <el-dialog v-model="dialogVisible">
      <img w-full :src="dialogImageUrl" alt="Preview Image" />
    </el-dialog>
    <el-button type="primary" @click="commit" :disabled="value===null || value===''">提交</el-button>

    <h2 :style="`font-size: var(--el-font-size-extra-large)`" style="font-size: 26px; color: #615f5f">用户端首页颜色设置</h2>


  </el-card>
</template>

<script setup>

import {onMounted, ref} from "vue";
import {Plus} from "@element-plus/icons-vue";
import {searchMerchants} from "@/api/merchant";
import {addImage, searchImage} from "@/api/image";
import {open_error, open_success} from "@/utils/common";

const fileList = ref([]);

const dialogImageUrl = ref("");

const dialogVisible = ref(false);

const merchants = ref([]);
// 存储现在查询的商家
const value = ref();

const handlePictureCardPreview = (uploadFile) => {
  console.log("aaa");
  dialogImageUrl.value = uploadFile.url;
  dialogVisible.value = true;
}


const check = () => {
  fileList.value = [];
  searchImage({id : value.value}).then((res) => {
    for(let item in res.data){
      fileList.value.push({
        name: "image",
        url: res.data[item].imagePath
      });
    }
    console.log(fileList.value);
  })
}

const commit = () => {
  const imageList = ref([]);
  console.log(fileList.value);
  for(let item in fileList.value) {
    imageList.value.push(fileList.value[item].response.data);
  }
  console.log(imageList.value);
  addImage(imageList.value, value.value).then((res) => {
    if(res.state === 0) {
      open_success("成功修改")
    } else {
      open_error(res.message);
    }
  })
}

onMounted(() => {
  searchMerchants().then((res) => {
    merchants.value = res.data;
  });
})

</script>
<style scoped>
.course {
  font-weight: bold;
  display: inline-block;
}
.course li {
  line-height: 36px;
  color: #409eff;
  cursor: pointer;
}
</style>